/**
 *Copyright [2010] [lcj(caijunlu@gmail.com)]
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License. 
 *You may obtain a copy of the License at 
 *             http://www.apache.org/licenses/LICENSE-2.0 
 *Unless required by applicable law or agreed to in writing, 
 *software distributed under the License is distributed on an "AS IS" BASIS, 
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 *either express or implied. See the License for the specific language governing permissions and limitations under the License
 */
package org.independent.servlet.session.interceptor;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.independent.servlet.session.ServletSession;
import org.independent.servlet.session.manager.SessionsManager;
import org.independent.servlet.session.service.ContextHolderFactory;

/**
 * The interceptor for intercept the Tomcat to get httpServletSession
 * 
 * @author lcj
 * 
 */
public aspect TomcatHttpSessionInterceptor {
	SessionsManager sessionManager = SessionsManager.getInstance();
	pointcut getSession():(call(public HttpSession javax.servlet.http.HttpServletRequest.getSession(boolean))
            ||call(public HttpSession javax.servlet.http.HttpServletRequest.getSession())
            ||execution(public HttpSession javax.servlet.http.HttpServletRequest.getSession(boolean))
            ||execution(public HttpSession javax.servlet.http.HttpServletRequest.getSession())
	        );
	
	public ServletSession javax.servlet.ServletRequest.servletSession;

	public ServletSession javax.servlet.ServletRequest.getServletSession() {
		return servletSession;
	}

	private void javax.servlet.ServletRequest.setServletSession(
			ServletSession session) {
		servletSession = session;
	}

	/**
	 * if the target is implement from HttpServletRequest, handle it by method
	 * "getSessionByRequest" else get the session from serviceContext See the
	 * code of org.independent.servlet.session.service.ServiceContext
	 */
	HttpSession around(): getSession(){
		ServletSession session =ContextHolderFactory.getServiceContextHolder().getContext()
		.getSession();
		System.out.println(" ContextHolderFactory.getServiceContextHolder()  getSession "
				+ session);
		if(session!=null)return session;
		if (thisJoinPoint.getTarget() instanceof HttpServletRequest) {
			HttpServletRequest request=(HttpServletRequest) thisJoinPoint.getTarget();
		System.out.println(" TomcatHttpSessionInterceptor  around  getSession "
				+ request);
		/**
		 * if the object of a request has session,sometime need the request's
		 * session again only by itself has
		 * 
		 * @see #javax.servlet.ServletRequest.getServletSession()
		 */
		session= request.getServletSession();
		if (session != null)
			return session;

		/**
		 * if in one thread get session by the request ,it only get from the
		 * threadLocal
		 */
		if (ContextHolderFactory.getServiceContextHolder().getContext()
				.getHasSession() == 1) {
			System.out.println("serviceContext has session,the id  is ...."
					+ ContextHolderFactory.getServiceContextHolder()
							.getContext().getSession().getId());
			return ContextHolderFactory.getServiceContextHolder().getContext()
					.getSession();
		}

		
			System.out
					.println("(HttpServletRequest) thisJoinPoint.getTarget() is "
							+ (HttpServletRequest) thisJoinPoint.getTarget());

			session = (ServletSession) sessionManager
					.getSessionByRequest(request);
			/**
			 * the object of request first get session ,the session Should be
			 * stored to the request
			 * 
			 * @see #javax.servlet.ServletRequest.setServletSession(ServletSession
			 *      session)
			 */
			request.setServletSession(session);
			System.out.println("request.getServletSession(session) "
					+ request.getServletSession());
			return session;
		}
		return null;

	}

}
